package nexus.com.container
{
	import com.ux.control.DragBarVertical;
	import com.ux.event.DragBarEvent;
	
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	
	import nexus.base.NXSprite;
	
	/**
	 * @author winnie
	 */	
	public class ScrollContainer extends Container
	{
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private
		//
		//-----------------------------------------------------------------------------------
		private var _enableVertivalScroll : Boolean = true;
		private var _enableHorizontalScroll : Boolean = true;
		
		private var _verticalScroll : DragBarVertical;
		private var _isNeedVscroll : Boolean = false;
		private var _scrollInterval : Number = 0;
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		protected var scrollVerticalValue : Number = 0;
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function ScrollContainer()
		{
			super();
			
			_verticalScroll = new DragBarVertical();
			_verticalScroll.track = new VERTICAL_LINE();
			_verticalScroll.btn = new VERTICAL_SCROLL_BTN();
			_verticalScroll.icon = new VERTICAL_SCROLL_ICON();
			_verticalScroll.addEventListener(DragBarEvent.VERTICAL_UPDATE, onVScrollEvent );
			addChild( _verticalScroll );
			
			this.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel );
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOver );
			verticalScrollSetting();
			
		}
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------

		

		public function get scrollInterval():Number
		{
			return _scrollInterval;
		}

		public function set scrollInterval(value:Number):void
		{
			_scrollInterval = value;
		}

		public function get verticalScroll():DragBarVertical
		{
			return _verticalScroll;
		}

		public function get isNeedVscroll():Boolean
		{
			return _isNeedVscroll;
		}

		public function get enableHorizontalScroll():Boolean
		{
			return _enableHorizontalScroll;
		}

		public function set enableHorizontalScroll(value:Boolean):void
		{
			_enableHorizontalScroll = value;
		}

		public function get enableVertivalScroll():Boolean
		{
			return _enableVertivalScroll;
		}

		public function set enableVertivalScroll(value:Boolean):void
		{
			_enableVertivalScroll = value;
		}
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// public
		//
		//-----------------------------------------------------------------------------------
		override public function updateDisplayList():void
		{
			super.updateDisplayList();
			
			setWrapConPosition();
			
			maskClip.x = paddingLeft + maskPaddingLeft;
			maskClip.y = paddingTop + maskPaddingTop;
			maskClip.width = viewWidth-paddingLeft-paddingRight - maskPaddingLeft - maskPaddingRight;
			maskClip.height = viewHeight-paddingTop-paddingBottom - maskPaddingTop - maskPaddingBottom;
			
			checkScrollPolicy();
			
		}
		
		public function checkScrollPolicy():void
		{
			_isNeedVscroll = ( wrapperContainer.height > viewHeight )?true:false;
			
			if( _isNeedVscroll )
			{
				if( this.contains( _verticalScroll ) == false )
					addChild( _verticalScroll );
				verticalScrollSetting();
			}
			else
			{
				if( this.contains( _verticalScroll ) == true )
					removeChild( _verticalScroll );
			}
		}
		
		/**
		 * 스크롤 및 스크롤에 따른 컨테이너 좌표 리셋.
		 */		
		public function resetScroll():void
		{
			_verticalScroll.reset();
		}
		
		/**
		 * 스크롤 상태에 따른 wrapperContainer 좌표 설정. 
		 */		
		public function setWrapConPosition():void
		{
			var originNum : Number = -( wrapperContainer.height - ( viewHeight-paddingTop-paddingBottom-maskPaddingTop-maskPaddingBottom ) )* scrollVerticalValue + paddingTop+maskPaddingTop;
			
//			var targetPoint : Number = 0;
//			if( Math.round( ( originNum % scrollInterval ) / scrollInterval ) == 0 )
//			{
//				targetPoint = originNum-( originNum % scrollInterval )
//			}
//			else
//			{
//				targetPoint = originNum-( originNum % scrollInterval ) + scrollInterval;
//			}
			
//			trace(" ==> ScrollContainer setWrapConPosition : " + originNum, targetPoint );
			
			if( _isNeedVscroll ) wrapperContainer.y = originNum;
			else wrapperContainer.y = paddingTop+maskPaddingTop;
			
//			wrapperContainer.y = targetPoint;
		}
		
		public function setScrollBg():void
		{
			wrapperContainer.setBg();
		}
		
		//-----------------------------------------------------------------------------------
		//
		// private funciton
		//
		//-----------------------------------------------------------------------------------
		
		
		private function verticalScrollSetting():void
		{
			this.setChildIndex( _verticalScroll, this.numChildren - 1 );
			_verticalScroll.x = viewWidth-_verticalScroll.width/2-paddingRight-maskPaddingRight;
			_verticalScroll.y = paddingTop;
			_verticalScroll.btn.x = -_verticalScroll.btn.width/2 + 1;
			_verticalScroll.track.height = viewHeight-paddingTop-paddingBottom;
			_verticalScroll.btnReSize( wrapperContainer.height );
		}
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		protected function onVScrollEvent(e:DragBarEvent):void
		{
			scrollVerticalValue = e.dragValue;
			setWrapConPosition();
		}
		
		
		protected function onMouseWheel(e:MouseEvent):void
		{
			// 일단 화면에 보이는 만큼 스크롤.
			if( _scrollInterval == 0 ) 
			{
				_scrollInterval = maskClip.height;
			}
			
			var interval : Number = ( _scrollInterval / wrapperContainer.height )*_scrollInterval;
			
			if( _isNeedVscroll )
			{
				if( e.delta > 0 )
				{
					_verticalScroll.btnMove( -interval );
				}
				else 
				{
					_verticalScroll.btnMove( interval );
				}
			}
		}	
		
		
		protected function onRollOver(e:MouseEvent):void
		{
			if( stage != null && stage.focus != this )
			{
				stage.stageFocusRect = false;
				stage.focus = this;
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
		override public function destory():void
		{
			super.destory();
		}

	}
}